package sk.vrto.web.presenters;

import sk.vrto.domain.Contact;
import sk.vrto.domain.User;
import sk.vrto.service.dao.ContactRepository;
import sk.vrto.web.base.Presenter;
import sk.vrto.web.events.ContactInsertedEvent;
import sk.vrto.web.views.NewContactView;

public class NewContactPresenter extends Presenter<NewContactView> {

    private final ContactRepository contactRepository;
    private final User user;

    /**
     * @param contactRepository Repository for accessing contacts
     * @param user Logged user
     */
    public NewContactPresenter(ContactRepository contactRepository, User user) {
        this.contactRepository = contactRepository;
        this.user = user;
    }

    /**
     * Creates and inserts new contact, and then propagates {@link ContactInsertedEvent}
     * @param name Contact's name
     * @param email Contact's email
     */
    public void createContact(String name, String email) {
        Contact contact = new Contact(name, email, user);
        contactRepository.insert(contact);
        eventBus.post(new ContactInsertedEvent(contact));
    }

}
